
import datasource

FORMAT_DAY = "%Y%m%d"
SELL_CODE = -1000000
BUY_CODE = 1000000

# 导入函数库
def single_macd(ts_code, n=5):
    # 获取股票的收盘价
    daily_price = datasource.daily(ts_code)
    # 取得过去 ma_n1 天的平均价格
    ma_before_yesterday = daily_price['close'][2:n+2].mean()
    ma_yesterday = daily_price['close'][1:n+1].mean()
    ma_today = daily_price['close'][:n].mean()

    # 判断凹凸
    if ma_yesterday < ma_before_yesterday and ma_yesterday < ma_today:
        return BUY_CODE
    elif ma_yesterday > ma_before_yesterday and ma_yesterday > ma_today:
        return SELL_CODE
    else:
        sign = 1 if ma_today-ma_yesterday > 0 else -1
        return round(sign * (ma_today-ma_yesterday)/(ma_yesterday-ma_before_yesterday), 4)

